package com.sx.zongzhi.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.HouseAdapter;
import com.sx.zongzhi.bean.HouseBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.UserPermission;
import com.sx.zongzhi.presenter.FactHousePest;
import com.sx.zongzhi.utils.AppConstants;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IFactHouseView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class BuildingSelectHouseActivity extends BaseActivity implements
        SwipeRefreshLayout.OnRefreshListener, IFactHouseView {

    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_Total)
    TextView tvTotal;
    @BindView(R.id.imvAdd)
    ImageView imvAdd;

    private HouseAdapter houseAdapter;
    private List<HouseBean> houseList = new ArrayList<>();

    private int page = 1, rows = 10;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_household);
        setTitleName(R.string.fact_house);
        ButterKnife.bind(this);
        initViews();

    }

    private void initViews() {
        imvAdd.setVisibility(View.GONE);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(this);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        houseAdapter = new HouseAdapter(this, R.layout.item_house_list, houseList);
        houseAdapter.setEmptyView(View.inflate(this, R.layout.load_error_view, null));
        recyclerView.setAdapter(houseAdapter);
        houseAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getData();
            }
        }, recyclerView);

        houseAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MyRxBus.get().post(new MsgEvent<>("BuildingLayoutActivity",1,houseList.get(position).getId()));
                finish();
            }
        });
        onRefresh();
    }



    @Override
    public void onRefresh() {
        swipeRefreshLayout.setRefreshing(true);
        page = 1;
        getData();
    }

    private void getData() {
        new FactHousePest(this).getFactHouseList(this, page, rows);
    }

    @Override
    public void getFactHouseListSuccess(UserPermission userPermissions, List<HouseBean> mailBeanLists, int total) {
        if (swipeRefreshLayout.isRefreshing()){
            swipeRefreshLayout.setRefreshing(false);
        }
        if (page == 1) {
            houseList.clear();
            houseAdapter.setNewData(houseList);
        }
        if (mailBeanLists != null && mailBeanLists.size() > 0) {
            houseList.addAll(mailBeanLists);
        }
        houseAdapter.notifyDataSetChanged();
        tvTotal.setText("共" + total + "条数据");
        if (houseList.size() == total){
            houseAdapter.loadMoreEnd();
        }else {
            houseAdapter.loadMoreComplete();
        }
    }

    @Override
    public void showError(String message) {
        if (swipeRefreshLayout.isRefreshing()){
            swipeRefreshLayout.setRefreshing(false);
        }
        ToastUtils.showToast(BuildingSelectHouseActivity.this, message);
    }

}
